package com.qyhl.module_practice.score.mine;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.module_practice.R;
import com.qyhl.module_practice.R2;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.UpTokenBean;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeTeamBean;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeVolunteerDetailBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.weavey.loading.lib.LoadingLayout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PracticeScoreMineFragment extends BaseFragment implements PracticeScoreMineContract.PracticeScoreMineView {
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    @BindView(R2.id.head_icon)
    RoundedImageView headIcon;
    @BindView(R2.id.nickName)
    TextView nickName;
    @BindView(R2.id.total_score)
    TextView totalScore;
    @BindView(R2.id.year_score)
    TextView yearScore;
    @BindView(R2.id.rank_score)
    TextView rankScore;
    @BindView(R2.id.captain_tag)
    TextView captainTag;
    @BindView(R2.id.identity_tag)
    ImageView identityTag;
    @BindView(R2.id.score_service_team)
    TextView scoreServiceTeam;
    @BindView(R2.id.divider1)
    View divider1;
    @BindView(R2.id.score_upload)
    TextView scoreUpload;
    @BindView(R2.id.divider3)
    View divider3;
    @BindView(R2.id.score_exchange)
    TextView scoreExchange;
    @BindView(R2.id.divider5)
    View divider5;
    @BindView(R2.id.score_integral)
    TextView scoreIntegral;
    @BindView(R2.id.divider6)
    View divider6;
    @BindView(R2.id.score_rank)
    RelativeLayout scoreRank;


    private View view;

    private PracticeScoreMinePresenter mPresenter;

    private String userName;

    private String volId;

    private int volStatus;

    private String instId;

    private PracticeVolunteerDetailBean mBean;

    private boolean isVolunteer;

    public void setInstId(String instId) {
        this.instId = instId;
    }

    public void setVolStatus(int volStatus) {
        this.volStatus = volStatus;
    }

    public void setVolId(String volId) {
        this.volId = volId;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }




    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return view = inflater.inflate(R.layout.practice_fragment_score_mine, null);
    }

    @Override
    protected void create() {
        mPresenter = new PracticeScoreMinePresenter(this);
        loadData();
    }

    @Override
    protected void setListener() {
        setListeners();
    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    public static PracticeScoreMineFragment newInstance(String userName, String volId, int volStatus, String instId) {
        PracticeScoreMineFragment fragment = new PracticeScoreMineFragment();
        fragment.setUserName(userName);
        fragment.setVolId(volId);
        fragment.setVolStatus(volStatus);
        fragment.setInstId(instId);
        return fragment;
    }


    /**
     * 加载数据
     */
    private void loadData() {
        //下拉刷新
        initView();
    }



    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);

        if (volStatus == 1) {
            isVolunteer = true;
            identityTag.setImageResource(R.drawable.practice_score_volunteer_tag_icon);

            scoreServiceTeam.setVisibility(View.VISIBLE);
            divider1.setVisibility(View.VISIBLE);

            scoreUpload.setVisibility(View.VISIBLE);
            divider3.setVisibility(View.VISIBLE);

            scoreExchange.setVisibility(View.VISIBLE);
            divider5.setVisibility(View.VISIBLE);

            scoreIntegral.setVisibility(View.VISIBLE);
            divider6.setVisibility(View.VISIBLE);

            scoreRank.setVisibility(View.VISIBLE);

            mPresenter.getDetail(volId);
        } else {
            isVolunteer = false;
            if (volStatus == 2) {
                identityTag.setImageResource(R.drawable.practice_score_judge_tag_icon);
            } else {
                identityTag.setImageResource(R.drawable.practice_score_people_tag_icon);
            }
            captainTag.setVisibility(View.GONE);

            scoreServiceTeam.setVisibility(View.GONE);
            divider1.setVisibility(View.GONE);

            scoreUpload.setVisibility(View.GONE);
            divider3.setVisibility(View.GONE);

            scoreExchange.setVisibility(View.GONE);
            divider5.setVisibility(View.GONE);

            scoreIntegral.setVisibility(View.GONE);
            divider6.setVisibility(View.GONE);

            scoreRank.setVisibility(View.GONE);

            String cover = CommonUtils.getInstance().getUserLogo();
            Glide.with(this)
                    .load(cover)
                    .apply(new RequestOptions().placeholder(R.drawable.comment_head_default).error(R.drawable.comment_head_default))
                    .into(headIcon);

            String name = CommonUtils.getInstance().getNickName();
            nickName.setText(name);

            loadMask.setStatus(LoadingLayout.Success);
        }

    }

    private void setListeners() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                mPresenter.getDetail(volId);
            }
        });
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void setDetail(PracticeVolunteerDetailBean bean) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");

        mBean = bean;

        Glide.with(this)
                .load(bean.getLogo())
                .apply(new RequestOptions().placeholder(R.drawable.comment_head_default).error(R.drawable.comment_head_default))
                .into(headIcon);

        nickName.setText(bean.getName());

        totalScore.setText(bean.getTotalScore() + "");

        yearScore.setText(bean.getAnnualScore() + "");

        rankScore.setText(bean.getRanking());

        if (StringUtils.isNotEmpty(bean.getRole()) && bean.getRole().equals("CAPTAIN")) {
            captainTag.setVisibility(View.VISIBLE);
        } else {
            captainTag.setVisibility(View.GONE);
        }

    }

    @Override
    public void setError(String msg) {
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~");
        if (!NetUtil.isNetworkAvalible(getContext())) {
            loadMask.setErrorImage(R.drawable.error_network);
            loadMask.setErrorText("网络异常，请检查您的网络！");
        } else if (msg.contains("暂无")) {
            loadMask.setErrorImage(R.drawable.empty_content);
            loadMask.setErrorText(msg);
        } else {
            loadMask.setErrorImage(R.drawable.error_content);
            loadMask.setErrorText(msg);
        }
    }

    @Override
    public void getTokenSuccess(UpTokenBean data, boolean isUpload) {

    }

    @Override
    public void getTokenError(boolean isUpload) {

    }

    @Override
    public void uploadSuccess(String msg) {

    }

    @Override
    public void uploadError(String msg) {

    }


    @OnClick({R2.id.score_rank, R2.id.score_integral, R2.id.score_service_team, R2.id.score_upload, R2.id.score_exchange, R2.id.score_act, R2.id.score_order, R2.id.head_icon})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.head_icon) {
            if (volStatus == 1) {
                Bundle bundle = new Bundle();
                bundle.putString("id", volId);
                RouterManager.router(ARouterPathConstant.PRACTICE_SCORE_RANK,bundle);
            }
        } else if (id == R.id.score_rank) {
            if (volStatus == 1) {

                Bundle bundle = new Bundle();
                bundle.putString("id", volId);
                RouterManager.router(ARouterPathConstant.PRACTICE_SCORE_RANK,bundle);
            }
        } else if (id == R.id.score_integral) {
            Bundle bundle = new Bundle();
            bundle.putString("id", volId);
            RouterManager.router(ARouterPathConstant.PRACTICE_SCORE_BILL,bundle);
        } else if (id == R.id.score_service_team) {
            if (mBean != null && mBean.getOrgs() != null && mBean.getOrgs().size() > 0) {
                if (mBean.getOrgs().size() == 1) {
                    Bundle bundle = new Bundle();
                    bundle.putString("volId", volId);
                    bundle.putString("orgId", mBean.getOrgs().get(0).getId() + "");
                    bundle.putString("instId", instId);
                    bundle.putString("title", "我的服务队");
                    RouterManager.router(ARouterPathConstant.PRACTICE_TEAM_DETAIL,bundle);
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("volId", volId);
                    bundle.putInt("status", volStatus);
                    bundle.putString("instId", instId);
                    bundle.putBoolean("isMine", true);
                    bundle.putSerializable("list", (Serializable) mBean.getOrgs());
                    RouterManager.router(ARouterPathConstant.PRACTICE_TEAM_LIST,bundle);
                }
            } else {
                showToast("您尚未加入任何服务队!", 4);
            }
        } else if (id == R.id.score_upload) {

            Bundle bundle = new Bundle();
            bundle.putString("volId", volId);
            bundle.putString("userName", userName);
            bundle.putString("title", "服务上传");
            bundle.putString("actId", "");
            RouterManager.router(ARouterPathConstant.PRACTICE_APPLY,bundle);

        } else if (id == R.id.score_exchange) {

            Bundle bundle = new Bundle();
            bundle.putString("volId", volId);
            RouterManager.router(ARouterPathConstant.PRACTICE_SCORE_MY_SHOP,bundle);
        } else if (id == R.id.score_act) {
            Bundle bundle = new Bundle();
            bundle.putString("id", volId);
            bundle.putString("userName", userName);
            bundle.putBoolean("isVolunteer", isVolunteer);
            RouterManager.router(ARouterPathConstant.PRACTICE_SCORE_MY_SHOP,bundle);
        } else if (id == R.id.score_order) {

            Bundle bundle = new Bundle();
            bundle.putString("id", volId);
            bundle.putString("userName", userName);
            bundle.putBoolean("isVolunteer", isVolunteer);
            bundle.putString("instId", instId);
            RouterManager.router(ARouterPathConstant.PRACTICE_ORDER_MINE,bundle);
        }
    }

}
